package org.itboys.mobile.controller.web;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.entity.mongo.SundayMember;
import org.itboys.mobile.service.mongo.web.SundayMemberService;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 作者：jiangxiong
 * 日期：2017年5月8日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_商品_controller_web
 *
 */
@Controller
@RequestMapping("/sunday/web/member")
public class SundayMemberController extends BaseController {

    @Autowired
    private SundayMemberService memberService;

    /**
     * 用户  主页
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/index" )
    public  String index(HttpServletRequest request,
                         @RequestParam(value="returnPage",required=false)Integer returnPage,
                           Model model){
        //门店用户主页
        if(returnPage==SundayCommonConstants.user_type_shop){
            return "/sunday/member/index_shop";
            //大客户用户主页
        }else if(returnPage== SundayCommonConstants.user_type_customer){
            return "/sunday/member/index_customer";
        }
        return "/sunday/member/index";

    }

    /**
     * 分页用户信息
     * @param request
     * @param response
     */
    @RequestMapping("/select")
    public void select(HttpServletRequest request,
                        HttpServletResponse response
    ) {
        try {
            PageResult<SundayMember> result=memberService.selectMember(request);
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
        } catch (Exception e) {
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }
    /**
     * 不分页用户信息
     * @param request
     * @param response
     */
    @RequestMapping("/selectNoPage")
    public void selectNoPage(HttpServletRequest request,
                       HttpServletResponse response,
                             @RequestParam(value="userTypes",required=false)List<Integer> userTypes
    ) {
        try {
            List<SundayMember> result=memberService.selectMemberNoPage(request,userTypes);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgResultDO(result,CommonConstants.SUCCESS));
        } catch (Exception e) {
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }

    /**
     * 广告位。单个获取
     * @param request
     * @param response
     */
    @RequestMapping("/findOne")
    public void findOne(HttpServletRequest request,
                             HttpServletResponse response,
                        @RequestParam(value="id",required=true)Long id,
                        @RequestParam(value="userType",required=false)Integer userType
    ) {
        try {
            SundayMember member = memberService.findOne(id,userType);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgResultDO(member, CommonConstants.SUCCESS));

        } catch (Exception e) {
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }
    /**
     * 新增或修改用户
     * @param request
     * @param response
     * @param member

     */
    @RequestMapping("/save")
    public  void save(HttpServletRequest request,
                      HttpServletResponse response,
                      @ModelAttribute SundayMember member
    ) {
        try {
            memberService.saveMember(member);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));

        } catch (Exception e) {
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/member/save------" + e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE,msg));
        }
    }
   /**
     * 重置用户密码
     * @param request
     * @param response
     * @param id
     */
    @RequestMapping("/resetPassword")
    public void resetPassword(HttpServletRequest request,
                              HttpServletResponse response,
                              @RequestParam(value="id",required=true) Long id

    ) {
        try {
            memberService.resetPassword(id);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
        } catch (Exception e) {
            e.printStackTrace();
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/member/resetPassword------" + e.getMessage());
            }
            e.printStackTrace();

            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }
    /**
     * 更改用户状态
     * @param request
     * @param response
     * @param id
     */
    @RequestMapping("/changeStatus")
    public void changeStatus(HttpServletRequest request,
                              HttpServletResponse response,
                              @RequestParam(value="id",required=true) Long id,
                              @RequestParam(value="status",required=true) Integer status


    ) {
        try {
            memberService.changeStatus(id,status);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
        } catch (Exception e) {
            e.printStackTrace();
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/member/changeStatus------" + e.getMessage());
            }
            e.printStackTrace();

            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }

    /**
     * 门店配送员主页
     * @param request
     * @param response
     * @param parentId
     * @param model
     * @return
     */
    @RequestMapping("/deliveryman/index")
    public String deliveryManIndex(HttpServletRequest request,
                                   HttpServletResponse response,
                                   @RequestParam(value="parentId",required=true)Long parentId,
                                   Model model){
        model.addAttribute("parentId",parentId);
        return "/sunday/member/deliveryman/index";
    }

    /**
     * 用户
     * @param request
     * @param isSingle
     * @param param
     * @param model
     * @return
     */
    @RequestMapping("/choose" )
    public  String choose(HttpServletRequest request,
                          @RequestParam(value="isSingle",required=true)Integer isSingle,//是否单选
                          @RequestParam(value="callBack",required=false)String callBack,//回调参数
                          @RequestParam(value="param",required=false)String param,//查询参数
                          Model model){
        model.addAttribute("param",param);
        model.addAttribute("callBack",callBack);
        if(isSingle==null){
            return "/sunday/member/choose/choose_erro";
        }else if(isSingle==1){
            return "/sunday/member/choose/choose_single";
        }else if(isSingle==0){
            return "/sunday/member/choose/choose_muilt";
        }

        return "/finance/member/choose/choose_erro";
    }
    /**
     * --------------POS相关---------------
     */

}
